import request from '@/utils/request'
import type { PaginatedResult, PaginationQuery } from '@/types/common'
import type {
  User,
  UserCreatePayload,
  UserUpdatePayload,
  Role,
  RolePayload,
  UserRole
} from '@/types/system'

export const getUserList = (params?: PaginationQuery) =>
  request.get<PaginatedResult<User>>('/api/v1/users/list', { params })

export const createUser = (payload: UserCreatePayload) =>
  request.post<User>('/api/v1/users/create', payload)

export const updateUser = (id: number, payload: UserUpdatePayload) =>
  request.put<User>(`/api/v1/users/${id}`, payload)

export const deleteUser = (id: number) => request.delete(`/api/v1/users/${id}`)

export const getRoleList = (params?: PaginationQuery) =>
  request.get<PaginatedResult<Role>>('/api/v1/roles/list', { params })

export const createRole = (payload: RolePayload) =>
  request.post<Role>('/api/v1/roles/create', payload)

export const updateRole = (id: number, payload: RolePayload) =>
  request.put<Role>(`/api/v1/roles/${id}`, payload)

export const deleteRole = (id: number) => request.delete(`/api/v1/roles/${id}`)

export const getUserRoleList = (params?: PaginationQuery) =>
  request.get<PaginatedResult<UserRole>>('/api/v1/user-roles/list', { params })

export const createUserRole = (payload: { user_id: number; role_id: number }) =>
  request.post<UserRole>('/api/v1/user-roles/create', payload)

export const updateUserRole = (id: number, payload: { user_id: number; role_id: number }) =>
  request.put<UserRole>(`/api/v1/user-roles/${id}`, payload)

export const deleteUserRole = (id: number) =>
  request.delete(`/api/v1/user-roles/${id}`)
